package com.sign.common.util.threadpool;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author 513422479@qq.com
 * Date on 2021/5/25  14:50
 */
@Configuration
public class ExecutePoolConfiguration {
    private int corePoolSize=16;
    private int maxPoolSize=32;
    private int queueCapacity=10000;
    private int keepAliveSecondes=3000;

    @Bean(name = "threadPoolTaskExecutor")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(corePoolSize);
        threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);
        threadPoolTaskExecutor.setQueueCapacity(queueCapacity);
        threadPoolTaskExecutor.setKeepAliveSeconds(keepAliveSecondes);

        // AbortPolicy 直接抛出异常
        // CallerRunsPolicy 主线程直接执行该任务，执行完成后尝试添加下一个任务到线程池中
        // DiscardPolicy 抛弃当前任务，不抛出异常
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        return threadPoolTaskExecutor;

    }
}
